

Page({
  data: {
    latitude: 39.909088,
    longitude: 116.397643,
    scale: 16,
    markers: [],
    searchValue: '公园',
    list: [
      '浙江省宁波市鄞州区万达公寓1幢506室',
      '浙江省宁波市鄞州区万达公寓1幢506室',
      '浙江省宁波市鄞州区万达公寓1幢506室',
      '浙江省宁波市鄞州区万达公寓1幢506室',
      '浙江省宁波市鄞州区万达公寓1幢506室',
      '浙江省宁波市鄞州区万达公寓1幢506室',
      '浙江省宁波市鄞州区万达公寓1幢506室',
      '浙江省宁波市鄞州区万达公寓1幢506室'
    ],
    // height:'height:100px'
  },
  setCenter() {

  },
  setMarkers(arr) {
    var _this = this
    var allMarkers = [{ id: 2, latitude: 39.909088, longitude: 116.397643, width: 30, height: 50 }]
    _this.setData({
      latitude: allMarkers[0].latitude,
      longitude: allMarkers[0].longitude,
      markers: allMarkers
    })
  },
  doInit() {
    // let MapContext = wx.createMapContext('map')
    // console.log(MapContext)
    // MapContext.on('markerClusterCreate',(res)=>{
    //   console.log(res)
    // })
  },

  getLocation() {
    console.log('getlocation')
    // wx.getLocation({
    //   type: 'gcj02', // 返回的经纬度坐标类型
    //   success: function (res) {
    //     console.log(res)
    //     return
    //     setTimeout(() => {
    //       this.setData({
    //         longitude: '116.491275',
    //       })
    //     }, 1000);
    //     this.setData({
    //       markers: [{
    //         // id: 0,
    //         longitude: '116.391275',
    //         latitude: '39.90765',
    //         // longitude: res.longitude,
    //         // latitude: res.latitude,
    //         // title: '我的位置',
    //         // iconPath: 'http://124.220.3.30:9000/miniapp-xjy-dev/public/base/back.png'
    //       }]
    //     });
    //   },
    //   fail(res) {
    //     console.log('fail')
    //     console.log(res)
    //   }
    // });
  },
  onRegionChange(e) {
    console.log(e)
    if (e.causedBy === 'drag') {
      // 处理拖拽事件逻辑
      console.log(e); // 可以打印出拖拽的详细参数，进行进一步处理
      var _this = this
      // var allMarkers = [{ id: 2, latitude: e.detail.centerLocation.latitude, longitude: e.detail.centerLocation.longitude, width: 30, height: 50 }]
      _this.setData({
        latitude: e.detail.centerLocation.latitude,
        longitude: e.detail.centerLocation.longitude,
        scale: e.detail.scale
        // markers: allMarkers
      })
    }
  },
  getLocationToAddress() {
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1?location=39.984154,116.307490&key=AH7BZ-IAV6L-HL2P2-MUMDB-3PVSJ-M3F5U&get_poi=1',
      method: 'GET',
      success: (res) => {
        console.log(res)
      },
      fail: (res) => {

      },
    })
    // https://apis.map.qq.com/ws/geocoder/v1?location=39.984154,116.307490&key=OM2BZ-EVQC3-RLA3U-RGTMZ-BKEMZ-74BUQ&get_poi=1
    // https://apis.map.qq.com/ws/geocoder/v1/?key=F3PBZ-BJ2EZ-CUXXE-7N526-B7553-2UB4O
  },
  doSearch() {
    //   this.setData({
    //     longitude: '117.491275',
    //   })
    // return
    // https://apis.map.qq.com/ws/place/v1/search?boundary=nearby(40.040589,116.273543,1000)&keyword=公园&page_size=10&page_index=1&key=请在此输入您的key
    const cs = {
      3: 1000000,
      4: 500000,
      5: 200000,
      6: 100000,
      7: 50000,
      8: 25000,
      9: 20000,
      10: 10000,
      11: 5000,
      12: 2000,
      13: 1000,
      14: 500,
      15: 200,
      16: 100
    }
    let data = this.data;
    let baseUrl = 'https://apis.map.qq.com/ws/place/v1/search?&page_size=10&page_index=1&key=AH7BZ-IAV6L-HL2P2-MUMDB-3PVSJ-M3F5U', latitude = data.latitude,
      longitude = data.longitude, scale = Math.round(data.scale);
    scale = scale > 16 ? 16 : scale;
    console.log(cs[scale]);
    let url = baseUrl + `&boundary=nearby(${latitude},${longitude},${scale})` + `&keyword=${data.searchValue}`
    // return
    console.log(url)
    wx.request({
      url: url,
      method: 'GET',
      success: (res) => {
        console.log(res)
      },
      fail: (res) => {

      },
    })
  },

  chooseLocation() {
    // wx.chooseLocation({
    //   latitude: 39.909088,
    //   longitude: 116.397643,
    //   success: ((res) => {
    //     console.log(res)
    //   })
    // })
  },
  getFuzzyLocation() {
    // wx.getFuzzyLocation({
    //   type: 'wgs84',
    //   success(res) {
    //     console.log(res)
    //     const latitude = res.latitude
    //     const longitude = res.longitude
    //   }
    // })
  },
  onShow() {
    console.log('xinpinshow-show')
    // this.getLocation();
    // this.chooseLocation();
    this.getFuzzyLocation();


  },
  onLoad() {
    // this.getLocation();
  },
})
